/*
 * DeliveryRemoteFactory.java
 *
 * Created on April 23, 2007, 12:22 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usyd.comp5348;

import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author david
 */
public class DeliveryRemoteFactory {
    
 private static final String NAME = "ejb/Delivery";
    
    private static String getBeanName() {
        return NAME;
    }
    
    Hashtable<String,DeliveryRemote> srs = null;
    
    private static DeliveryRemoteFactory instance = null;
    
    private InitialContext context = null;
    
    /**
     * Creates a new instance of StockRemoteFactory
     */
    private DeliveryRemoteFactory() throws NamingException {
        context = new InitialContext();
        srs = new Hashtable<String,DeliveryRemote>();
    }
    
    public static DeliveryRemoteFactory getInstance() throws NamingException {
        if (instance==null)
            instance = new DeliveryRemoteFactory();
        return instance;
    }
    
    public DeliveryRemote getDeliveryRemote(String remoteName) throws NamingException {
        DeliveryRemote sr = srs.get(remoteName);
        if (sr==null) {
            context.addToEnvironment("org.omg.CORBA.ORBInitialHost",remoteName);
            context.addToEnvironment("org.omg.CORBA.ORBInitialPort","3700");
            context.addToEnvironment("java.naming.provider.url", "iiop://" + remoteName +":3700");
            sr = srs.put(remoteName, (DeliveryRemote)context.lookup(getBeanName()));
        }
        return sr;
    }
    
}
